package selection;

import java.util.ArrayList;
import java.util.List;

import GA.Chromosome;
import Operations.ValuesGenerator;
import util.GAConfig;

public class TournamentSelection extends ParentSelector{
	private static final int POPULATION_SIZE = GAConfig.PopulationSize;

	@Override
	public int selectTwoParents() {
		int tournamentSize=2;
		int[] selected = new int[tournamentSize];
		
			 // Pick two candidates at random.
			int pos1 = ValuesGenerator.getRandomInt(POPULATION_SIZE);
			int pos2 = ValuesGenerator.getRandomInt(POPULATION_SIZE);
		    while ((pos1 == pos2) && (POPULATION_SIZE>1)) {
		        pos2 = ValuesGenerator.getRandomInt(POPULATION_SIZE);
		      }

			float candidate1Fitness= super.gaInfo.getPopulation()[pos1].getFitness();
			float candidate2Fitness= super.gaInfo.getPopulation()[pos2].getFitness();
			//gaInfo.getPopulation();
            // Select the fitter candidate.
			if(candidate1Fitness>candidate2Fitness)
				return pos1;
			else if (candidate2Fitness>candidate1Fitness)
					return pos2;
				  else if (ValuesGenerator.getRandomDouble()<0.5)
					  		return pos1; 
		                else
		                	return pos2;


	}

}
